﻿using FluentValidation.AspNetCore;

using MediatR;
using MediatR.Pipeline;

using Microsoft.Extensions.DependencyInjection;

using System.Reflection;

namespace Holla.Infrastructure.Mediator.AspNet
{
    public static class MediatorAspNetExtension
    {
        public static IServiceCollection ConfigureMediator(this IServiceCollection services, Assembly applicationAssembly)
        {
            services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>))
                .AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehavior<,>))
                .AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>))
                .AddMediatR(applicationAssembly)
                .AddFluentValidation(fv => fv.RegisterValidatorsFromAssembly(applicationAssembly));

            return services;
        }
    }
}
